package com.common.app.utls

import android.text.InputFilter
import android.text.Spanned
import java.util.regex.Matcher
import android.text.TextUtils
import com.common.base.utils.MathUtil
import com.common.base.utils.ToastUtils
import java.util.regex.Pattern

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：7/8/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */

class CashierInputComFilterMinMax4 : InputFilter {
    private var MAX_VALUE = 100000000.00
    private var MIM_VALUE = 0.01
    private var MIM_VALUE_MSG = ""

    //小数点后的位数
    private var POINTER_LENGTH = 2
    private val POINTER = "."
    private val ZERO = "0"

    var mPattern: Pattern

    constructor(mimValue: Double) {
        mPattern = Pattern.compile("([0-9]|\\.)*")
        MIM_VALUE = mimValue
    }
    constructor(mimValue: Double,pointerLength:Int) {
        mPattern = Pattern.compile("([0-9]|\\.)*")
        MIM_VALUE = mimValue
        POINTER_LENGTH = pointerLength
    }

    constructor(mimValue: Double, msg: String) {
        mPattern = Pattern.compile("([0-9]|\\.)*")
        MIM_VALUE = mimValue
        MIM_VALUE_MSG = msg
    }

    constructor(mimValue: Double,maxValue: Double) {
        mPattern = Pattern.compile("([0-9]|\\.)*")
        MAX_VALUE = maxValue
        MIM_VALUE = mimValue
    }


    constructor(mimValue: Double,maxValue: Double,pointerLength:Int) {
        mPattern = Pattern.compile("([0-9]|\\.)*")
        MAX_VALUE = maxValue
        MIM_VALUE = mimValue
        POINTER_LENGTH = pointerLength
    }

    constructor(mimValue: Double,maxValue: Double, msg: String) {
        mPattern = Pattern.compile("([0-9]|\\.)*")
        MAX_VALUE = maxValue
        MIM_VALUE = mimValue
        MIM_VALUE_MSG = msg
    }

    /**
     * @param source 新输入的字符串
     * @param start  新输入的字符串起始下标，一般为0
     * @param end    新输入的字符串终点下标，一般为source长度-1
     * @param dest   输入之前文本框内容
     * @param dstart 原内容起始坐标，一般为0
     * @param dend   原内容终点坐标，一般为dest长度-1
     * @return 输入内容
     */
    override fun filter(
        source: CharSequence,
        start: Int,
        end: Int,
        dest: Spanned,
        dstart: Int,
        dend: Int
    ): CharSequence {
        val sourceText = source.toString()
        val destText = dest.toString()

        //验证删除等按键
        if (TextUtils.isEmpty(sourceText)) {
            return ""
        }
        val matcher: Matcher = mPattern.matcher(source)
        //已经输入小数点的情况下，只能输入数字
        if (destText.contains(POINTER)) {
            if (!matcher.matches()) {
                return ""
            } else {
                if (POINTER == source.toString()) {  //只能输入一个小数点
                    return ""
                }
            }

            //验证小数点精度，保证小数点后只能输入两位
            val index = destText.indexOf(POINTER)
            val length = dend - index
            if (length > POINTER_LENGTH) {
                return dest.subSequence(dstart, dend)
            } else {
                when (POINTER_LENGTH){
                    2->{
                        if ("0.0" == destText) {
                            var minValue = 0.0
                            if (!TextUtils.isEmpty(destText)) {
                                minValue = (destText + source.toString()).toDouble()
                            }
                            if (minValue < MIM_VALUE) {
                                if (!TextUtils.isEmpty(MIM_VALUE_MSG)) {//自定义提示不为空就用自定义提示
                                    if (MIM_VALUE == MAX_VALUE) {
                                        ToastUtils.showToast("只能输入${
                                            MathUtil.printNoMoreZero(MIM_VALUE)}！")
                                    } else {
                                        ToastUtils.showToast(MIM_VALUE_MSG)
                                    }
                                } else {
                                    ToastUtils.showToast("最小值不能小于${MathUtil.printNoMoreZero(MIM_VALUE)}！")
                                }
                                return ""
                            }
                        }
                    }
                    3->{
                        if ("0.00" == destText) {
                            var minValue = 0.00
                            if (!TextUtils.isEmpty(destText)) {
                                minValue = (destText + source.toString()).toDouble()
                            }
                            if (minValue < MIM_VALUE) {
                                if (!TextUtils.isEmpty(MIM_VALUE_MSG)) {//自定义提示不为空就用自定义提示
                                    if (MIM_VALUE == MAX_VALUE) {
                                        ToastUtils.showToast("只能输入${
                                            MathUtil.printNoMoreZero(MIM_VALUE)}！")
                                    } else {
                                        ToastUtils.showToast(MIM_VALUE_MSG)
                                    }
                                } else {
                                    ToastUtils.showToast("最小值不能小于${MathUtil.printNoMoreZero(MIM_VALUE)}！")
                                }
                                return ""
                            }
                        }
                    }
                    4->{
                        if ("0.000" == destText) {
                            var minValue = 0.000
                            if (!TextUtils.isEmpty(destText)) {
                                minValue = (destText + source.toString()).toDouble()
                            }
                            if (minValue < MIM_VALUE) {
                                if (!TextUtils.isEmpty(MIM_VALUE_MSG)) {//自定义提示不为空就用自定义提示
                                    if (MIM_VALUE == MAX_VALUE) {
                                        ToastUtils.showToast("只能输入${
                                            MathUtil.printNoMoreZero(MIM_VALUE)}！")
                                    } else {
                                        ToastUtils.showToast(MIM_VALUE_MSG)
                                    }
                                } else {
                                    ToastUtils.showToast("最小值不能小于${MathUtil.printNoMoreZero(MIM_VALUE)}！")
                                }
                                return ""
                            }
                        }
                    }
                }

            }
        } else {
            /**
             * 没有输入小数点的情况下，只能输入小数点和数字
             * 1. 首位不能输入小数点
             * 2. 如果首位输入0，则接下来只能输入小数点了
             */
            if (!matcher.matches()) {
                return ""
            } else {
                if (POINTER == source.toString() && TextUtils.isEmpty(destText)) {  //首位不能输入小数点
                    return ""
                } else if (POINTER != source.toString() && ZERO == destText) { //如果首位输入0，接下来只能输入小数点
                    return ""
                }
            }
        }

        //验证输入金额的大小
        val sumText = (destText + sourceText).toDouble()
        if (sumText > MAX_VALUE) {
            if (!TextUtils.isEmpty(MIM_VALUE_MSG)) {//自定义提示不为空就用自定义提示
                if (MIM_VALUE == MAX_VALUE) {
                    ToastUtils.showToast("只能输入${MathUtil.printNoMoreZero(MIM_VALUE)}！")
                } else {
                    ToastUtils.showToast(MIM_VALUE_MSG)
                }
            } else {
                ToastUtils.showToast("输入过大，请输入${MathUtil.printNoMoreZero(MAX_VALUE)}以内数值！")
            }
            return dest.subSequence(dstart, dend)
        }
        return dest.subSequence(dstart, dend).toString() + sourceText
    }
}